//
//  ViewController.m
//  UI_14_下午练习
//
//  Created by li cheng on 16/8/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "CollectionViewCell.h"
#import "CollectionReusableView.h"
#import "LabelAndCollectionReusableView.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSArray *arrayImagViewName;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.bounds));
    layout.itemSize = CGSizeMake( (width - 10) / 3, width);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 0;
//    layout.sectionInset = UIEdgeInsetsMake(7.5, 5, 7.5, 5);
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.headerReferenceSize = CGSizeMake(0, 50);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 400) collectionViewLayout:layout];
    collectionView.pagingEnabled = YES;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    // 通过上边距,实现视图添加(UICollectionView本质是scrollView)
    collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    [self.view addSubview:collectionView];
    [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    [collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1"];
    [collectionView registerClass:[LabelAndCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LabelAndCollectionReusableView"];
    _arrayImagViewName = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8",@"9", @"10", @"11", @"12", nil];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -295, self.view.bounds.size.width, 290)];
    view.backgroundColor = [UIColor purpleColor];
    [collectionView addSubview:view];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 12;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    cell.imageView.image = [UIImage imageNamed:_arrayImagViewName[indexPath.row]];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LabelAndCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LabelAndCollectionReusableView" forIndexPath:indexPath];
        return view;
    } else {
        CollectionReusableView *reuesableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1" forIndexPath:indexPath];
        reuesableView.backgroundColor = [UIColor yellowColor];
        return reuesableView;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
